package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.db.Authentication;
import com.apiit.webserviceclient.db.Authentication_DAO;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;
import com.apiit.webserviceclient.utility.GlobalData;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LMSLogin extends Activity implements OnClickListener {

	private Authentication_DAO datasource;

	EditText username;
	EditText pass;
	Button button;
	AlertDialog ad;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lmsloging);

		final Intent i = new Intent(this, OPACSearchResult.class);
		username = (EditText) findViewById(R.id.editTxtLmsusername);
		pass = (EditText) findViewById(R.id.EditTxtLmspassword);
		button = (Button) findViewById(R.id.btnLmsLogin);
		button.setOnClickListener(this);
		ad = new AlertDialog.Builder(this).create();
		datasource = new Authentication_DAO(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lmsloging, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		if (v == button) {
			datasource.open();
			try {
				BPEL_Process bpelProcess = new BPEL_Process();
				BPEL_Request request = new BPEL_Request();
				request.setUrl("http://lms.apiit.lk/login/index.php");

				Map<String, String> xpathMap = new HashMap<String, String>();
				xpathMap.put(
						"text",
						"/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]---");
				request.setXpathMap(xpathMap);
				Map<String, String> parametersMap = new HashMap<String, String>();
				request.setParametersMap(parametersMap);
				request.setType("GET");
				BPEL_Response respones = null;

				respones = bpelProcess.doRequest(request);

				BPEL_Request request2 = new BPEL_Request();
				String url_lib_auth = "http://lms.apiit.lk/login/index.php";
				request2.setUrl(url_lib_auth);

				Map<String, String> xpathMap2 = new HashMap<String, String>();
				xpathMap2.put("logout",
						"/html[1]/body[1]/div[2]/div[2]/div[1]/div[1]/a[2]---");
				xpathMap2
						.put("link",
								"/html[1]/body[1]/div[2]/div[2]/div[1]/div[1]/a[1]---href");
				request2.setXpathMap(xpathMap2);
				Map<String, String> parametersMap2 = new HashMap<String, String>();
				// username=cb004229&password=aaAA12!@
				parametersMap2.put("username", username.getText().toString());
				parametersMap2.put("password", pass.getText().toString());

				request2.setParametersMap(parametersMap2);
				request2.setType("POST");
				BPEL_Response respones2 = null;

				respones2 = bpelProcess.doRequest(request2);

				String testResult = respones2.getResultsMap().get("logout");
				String link = respones2.getResultsMap().get("link");

				if (respones2.getStatus().equalsIgnoreCase(
						GlobalData.message_success)) {
					if (datasource
							.getAction(GlobalData.authentication_id_lmsnew_perUser) == null) {
						Authentication authenticaton = new Authentication();
						authenticaton
								.setAuthentication_id(GlobalData.authentication_id_lmsnew_perUser);
						authenticaton
								.setauthentication_name(GlobalData.authentication_name_lmsnew);
						authenticaton.setuser_name(username.getText()
								.toString());
						authenticaton.setpassword(pass.getText().toString());
						datasource.createAction(authenticaton);
					} else {
						Authentication authenticaton = new Authentication();
						authenticaton.setuser_name(username.getText()
								.toString());
						authenticaton.setpassword(pass.getText().toString());
						datasource.updateAuthenticationDetails(
								GlobalData.authentication_id_lmsnew_perUser,
								GlobalData.authentication_name_lmsnew, username
										.getText().toString(), pass.getText()
										.toString());

					}

					Toast.makeText(getApplicationContext(), testResult,
							Toast.LENGTH_LONG).show();
					datasource.close();
					Intent i = new Intent(this, LMS.class);
					i.putExtra("logout", testResult);
					i.putExtra("link", link);
					startActivity(i);

				} else {
					AlertDialog.Builder alt = new
							AlertDialog.Builder(this);
					alt.setTitle("Error...");
					alt.setMessage("The username or password you entered is incorrect");
					alt.setNeutralButton("Close", null);
					alt.show();
					Intent i = new Intent(this, LMSLogin.class);
					startActivity(i);
				}

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
